package com.dl.base.ui.image

import android.graphics.drawable.Drawable
import android.net.Uri
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions

fun imageLoader(): ImageLoader = ImageLoaderImpl

object ImageLoaderImpl : ImageLoader {
    override fun load(url: String, imageView: ImageView) {
        Glide.with(imageView).load(url).into(imageView)
    }

    override fun load(
        url: String,
        holderResourceId: Int,
        errorResourceId: Int,
        imageView: ImageView
    ) {
        Glide.with(imageView).load(url)
            .apply(RequestOptions().placeholder(holderResourceId).error(errorResourceId))
            .into(imageView)
    }

    override fun load(uri: Uri, imageView: ImageView) {
        Glide.with(imageView).load(uri).into(imageView)
    }

    override fun load(resId: Int, imageView: ImageView) {
        Glide.with(imageView).load(resId).into(imageView)
    }

    override fun load(
        url: String,
        holderDrawable: Drawable?,
        errorDrawable: Drawable?,
        imageView: ImageView
    ) {
        Glide.with(imageView).load(url)
            .apply(RequestOptions().placeholder(holderDrawable).error(errorDrawable).centerCrop())
            .into(imageView)
    }

    override fun load(
        url: String,
        holderDrawable: Drawable?,
        errorDrawable: Drawable?,
        imageView: ImageView,
        radius: Int
    ) {

        val requestOptions =
            if (radius > 0) RequestOptions.bitmapTransform(RoundedCorners(radius)) else RequestOptions()


        Glide.with(imageView).load(url)
            .apply(
                requestOptions.placeholder(holderDrawable).error(
                    errorDrawable
                ).fitCenter()
            )
            .into(imageView)
    }

    override fun loadThumbnail(url: String, imageView: ImageView, width: Int, height: Int) {
        Glide.with(imageView).load(url)
            .apply(RequestOptions().override(width, height))
            .into(imageView)
    }

    override fun loadThumbnail(
        url: String,
        holderResourceId: Int,
        errorResourceId: Int,
        imageView: ImageView, width: Int, height: Int
    ) {
        Glide.with(imageView).load(url)
            .apply(
                RequestOptions().override(width, height).placeholder(holderResourceId).error(
                    errorResourceId
                )
            )
            .into(imageView)
    }
}